<template>
  <div class="app-container">
    <h2 style="text-align: center;">发布新课程</h2>
    <el-steps :active="2" process-status="wait" align-center style="margin-bottom: 40px;">
      <el-step title="填写课程基本信息"/>
      <el-step title="创建课程大纲"/>
      <el-step title="最终发布"/>
    </el-steps>

    <!--添加章节-->
    <el-button type="text" @click="addChapterInfo" style="font-size: 20px;">➡添加章节</el-button>
    <el-dialog :visible.sync="dialogChapterFormVisible" title="添加章节">
      <el-form :model="chapter" label-width="120px">
        <el-form-item label="章节标题">
          <el-input v-model="chapter.title"/>
        </el-form-item>
        <el-form-item label="章节排序">
          <el-input-number v-model="chapter.sort" :min="0" controls-position="right"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogChapterFormVisible = false">取消</el-button>
        <el-button type="primary" @click="saveOrUpdate()">确定</el-button>
      </div>
    </el-dialog>
    <!--章节部分-->
    <ul>
      <li v-for="chapter in chapterVideoList" :key="chapter.id">
        {{ chapter.title }}
        <span><el-button type="text" class="span1" @click="editChapter(chapter.id)">编辑</el-button></span>
        <span><el-button type="text" class="span2" @click="deleteChapter(chapter.id)">删除</el-button></span>
        <span><el-button type="text" class="span5" @click="openVideo(chapter.id)">添加小节</el-button></span>
        <!--小节部分-->
        <ul>
          <li v-for="video in chapter.children" :key="video.id">
            {{ video.title }}
            <span><el-button type="text" class="span3" @click="openEditVideo(video.id)" style="color: #5a5e66"
            >编辑</el-button></span>
            <span><el-button type="text" class="span4" @click="deleteVideo(video.id)" style="color: #5a5e66"
            >删除</el-button></span>
          </li>
        </ul>
      </li>
    </ul>

    <!--添加小节-->
    <el-dialog :visible.sync="dialogVideoFormVisible" title="添加小节">
      <el-form :model="video" label-width="120px">
        <el-form-item label="小节标题">
          <el-input v-model="video.title"/>
        </el-form-item>
        <el-form-item label="小节排序">
          <el-input-number v-model="video.sort" :min="0" controls-position="right"/>
        </el-form-item>

        <el-form-item label="是否免费">
          <el-radio-group v-model="video.free">
            <el-radio :label="true">免费</el-radio>
            <el-radio :label="false">默认</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="上传视频">
          <!--  TODO  -->
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVideoFormVisible = false">取消</el-button>
        <el-button type="primary" @click="saveOrUpdateVideo()">确定</el-button>
      </div>
    </el-dialog>

    <el-form label-width="120px">
      <el-form-item>
        <el-button @click="previous">上一步</el-button>
        <el-button :disabled="saveBtnDisabled" type="primary" @click="next">下一步
        </el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import chapter from '@/api/edu/chapter'
import video from '@/api/edu/video'

export default {
  name: 'Chapter',
  data() {
    return {
      saveBtnDisabled: false,
      chapterVideoList: [],
      courseId: '',
      chapterId: '',
      dialogChapterFormVisible: false,
      dialogVideoFormVisible: false,
      chapter: {
        title: '',
        sort: 0
        // courseId: ''
      },
      video: {
        // 小节对象
        // courseId: '',
        // chapterId: '',
        title: '',
        sort: 0,
        isFree: 0
      }
    }
  },
  created() {
    console.log(this.$route.query.courseId)
    this.init()
    this.getChapterVideo()

    if (this.$route.query && this.$route.query.courseId) {
      this.courseId = this.$route.query.courseId
      this.getChapterVideo()
    } else {
      console.log('no chapterId')
    }
  },
  methods: {
    //=========================================小节=======================================================//
    // 更新小节
    updateVideo() {
      video.updateVideo(this.video)
        .then(() => {
          this.$message.success('编辑小节成功～')
          this.getChapterVideo()
        })
        .catch(() => {
          this.$message.error('编辑小节失败!')
        })
    },
    // 查询小节
    selectVideoById(videoId) {
      video.selectVideoById(videoId)
        .then((response) => {
          this.video = response.data.video
        })
    },
    // 打开编辑小节按钮 查询小节信息回显
    openEditVideo(videoId) {
      // TODO radio未做到回显
      this.dialogVideoFormVisible = true
      this.selectVideoById(videoId)

    },
    // 删除小节
    deleteVideo(videoId) {
      video.deleteVideo(videoId)
        .then(() => {
          this.$message.success('删除小节成功～')
          this.getChapterVideo()
        })
        .catch(() => {
          this.$message.error('删除小节失败!')
        })
    },
    // 点击添加小节按钮
    openVideo(chapterId) {
      this.video = {}
      this.dialogVideoFormVisible = true
      this.video.courseId = this.courseId
      this.video.chapterId = chapterId
      // console.log(chapterId)
    },
    //添加小节
    addVideo() {
      // this.dialogVideoFormVisible = false
      video.addVideo(this.video)
        .then(() => {
          this.$message.success('添加小节成功～')
          this.getChapterVideo()
        })
        .catch(() => {
          this.$message.error('添加小节失败!')
        })
    },
    saveOrUpdateVideo() {
      this.dialogVideoFormVisible = false
      if (this.video.id) {
        this.updateVideo()
      } else {
        this.addVideo()
      }
    },

    //=========================================章节=======================================================//
    // 点击添加章节
    addChapterInfo() {
      this.dialogChapterFormVisible = true
      this.chapter.title = ''
      this.chapter.sort = 0
      this.chapter.id = ''
    },
    init() {
      this.courseId = this.$route.query.courseId
      this.chapter.courseId = this.courseId
    },
    // 获取课程大纲列表，根据课程id进行查询
    async getChapterVideo() {
      const result = await chapter.getChapterVideo(this.$route.query.courseId)
      // const result = await chapter.getChapterVideo(18)  // 这里先写死
      this.chapterVideoList = result.data.allChapterVideoList
      // console.log(result)
      // console.log(this.chapterVideoList)
    },
    previous() {
      // console.log(this.$route.query.id)
      this.$router.push({ path: '/course/info', query: { courseId: this.$route.query.courseId } })

      // console.log(this.$route.params.id)
      // this.$router.push({path: '/course/info/', params: {id: this.$route.params.id}})
    },
    next() {

      this.$router.push({ path: '/course/publish', query: { courseId: this.courseId } })
      // this.$router.push({ name: 'publish', params: { courseId: this.courseId } })

    },
    // 添加章节
    addChapter() {
      chapter.addChapter(this.chapter)
        .then(response => {
          // console.log(response)
          if (response.code === 20000) {
            this.$message.success('添加章节成功～')
          }
          this.getChapterVideo()
        })
        .catch(error => {
          this.$message.error('添加章节失败!')
        })
    },
    // 根据章节id查询
    async getChapterInfoById(chapterId) {
      const result = await chapter.getChapterInfoById(chapterId)
      this.chapter = result.data.chapter
    },

    // 修改章节
    async updateChapter() {
      const result = await chapter.updateChapter(this.chapter)
      if (result.code === 20000) {
        this.$message.success('修改章节成功～')
        await this.getChapterVideo()
      }
    },

    // 删除章节
    deleteChapter(chapterId) {
      chapter.deleteChapter(chapterId)
        .then((res) => {
          if (res.code === 20000) {
            this.$message.success('删除章节成功～')
            this.getChapterVideo()
          }
        })
        .catch((error) => {
          console.log(error)
          this.$message.error('删除章节失败！')
        })
    },

    saveOrUpdate() {
      this.dialogChapterFormVisible = false
      if (!this.chapter.id) {
        this.addChapter()
      } else {
        this.updateChapter()
      }
      this.getChapterVideo()
    },

    // 编辑章节
    editChapter(chapterId) {
      this.dialogChapterFormVisible = true
      // console.log(chapterId)
      this.getChapterInfoById(chapterId)
    }
  }
}
</script>

<style lang="less" scoped>
ul li {
  position: relative;
  margin: 10px;
  font-size: 20px;

  .span1 {
    position: absolute;
    right: 15%;
  }

  .span2 {
    position: absolute;
    right: 11%;
  }

  .span3 {
    position: absolute;
    right: 24%;
  }

  .span4 {
    position: absolute;
    right: 20%;
  }

  .span5 {
    position: absolute;
    right: 18.5%;
  }
}

</style>
